package GUI;

import Communications.SendData;
import Listeners.*;

import org.jdesktop.swingx.*;

import javax.swing.*;
import java.awt.*;
import java.io.IOException;

/**
 *
 * Created By: Richard Allsopp, Daniel Gadd, Dainius Narsutis
 * Date: 14/03/11
 *
 * Main Frame window which handles all other panels
 */
public class MainFrame extends JFrame{

    static JFrame mainFrame = new JFrame("EBOOK SHOP");
    private static JXPanel content = new JXPanel();
    public static int state = 0;
    private static LoginPane loginPane;
    private static SearchPane searchPane;
    private static RegisterPane registerPane;
    private static char connected;
    private static JPanel status = new JPanel();
    private static JLabel statusText = new JLabel();
    public static SendData communicate;
    private final static String STATUSLABEL = "Status: ";

    static {
        try {
            communicate = new SendData();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public MainFrame() throws IOException {

        setup();

        mainFrame.setSize(800,400);

        mainFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);

        mainFrame.setLocationRelativeTo(null);

        mainFrame.setVisible(true);

    }
    private void setup() throws IOException {

        JButton login = new JButton("Login");
        JButton search = new JButton("Search");
        JButton register = new JButton("Register");
        JButton exit = new JButton("Exit");

        updateStatus("Not Connected");

        JPanel menu = new JPanel(new GridLayout(0,1));

        menu.add(login);
        menu.add(search);
        menu.add(register);
        menu.add(exit);

        login.addActionListener(new LoginPaneListener());
        search.addActionListener(new SearchPaneListener());
        register.addActionListener(new RegisterPaneListener());
        exit.addActionListener(new ExitListener());

        mainFrame.add(content, BorderLayout.CENTER);
        mainFrame.add(menu,BorderLayout.WEST);

        connected = communicate.sendData();

        status.add(statusText);
        mainFrame.add(status,BorderLayout.SOUTH);

        if(connected == 'Y'){
            updateStatus("Connected");
        }
        else{
            JOptionPane.showMessageDialog(null,"Error With Connection Client Side (Server Did Not Respond With \'Y\' For The Handshake)\nServer Might NOT Be Online");
        }
    }
    public static void updateStatus(String s){

        statusText.setText("");
        statusText.setText(STATUSLABEL + s);
        status.repaint();
        status.updateUI();
    }
    public static void updateContent(JPanel things){

        mainFrame.add(things,BorderLayout.CENTER);

    }
    public static void loginPanel(){

        content.removeAll();

        loginPane = new LoginPane();

        content.add(loginPane,new BorderLayout());

        content.repaint();

        content.updateUI();

    }
    public static void searchPane(){

        content.removeAll();

        searchPane = new SearchPane();

        content.add(searchPane,BorderLayout.CENTER);

        content.repaint();

        content.updateUI();

    }
    public static void registerPane(){

        content.removeAll();

        registerPane = new RegisterPane();

        content.add(registerPane,BorderLayout.CENTER);

        content.repaint();

        content.updateUI();

    }
    public static void exitAction(){
        System.exit(0);
    }
}

